package com.fy.login.internal;

import com.fy.login.NetMetadata;

import javax.security.auth.callback.*;
import javax.security.auth.login.LoginException;

/**
 * 通过网络登陆使用的 PrincipalModule
 * @author zhufeifei 2022/8/9
 * @see AbstractLoginModule
 **/

public class NetLoginModule extends AbstractLoginModule {

    public NetLoginModule() throws LoginException {
        super();
    }

    @Override
    public boolean login() throws LoginException {
        MetadataCallback<?> metadataCallback = new MetadataCallback<>();
        PasswordCallback passwordCallback = new PasswordCallback("password", false);

        handlerCallback(callbackHandler, metadataCallback, passwordCallback);

        return checkLogin(metadataCallback.getMetadata(), passwordCallback.getPassword());
    }

    @Override
    public boolean logout() throws LoginException {
        MetadataCallback<?> metadataCallback = new MetadataCallback<>();
        handlerCallback(callbackHandler, metadataCallback);
        handlerLogout(metadataCallback.getMetadata());
        return true;
    }


}
